package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;


/*
    学生业务层实现类
*/
public class StudentServiceImpl implements StudentService {

    /*
        分页查询功能
    */
    @Override
    public Page selectByPage(Integer currentPage, Integer pageSize)  {
        InputStream is =null;
        SqlSession sqlSession =null;
        Page page =null;


        try {
            //        加载核心配置文件
            is= Resources.getResourceAsStream("MyBatisConfig.xml");
//            2.获取sqlsession对象工厂对象
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
//            3.通过sqlsession对象获取工厂对象
            sqlSession=build.openSession(true);
//            4.获取studentmapper接口的实现类对象
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//            5.设置分页参数
            page = PageHelper.startPage(currentPage, pageSize);
//            6.调用实现类查询全部方法
            mapper.selectAll();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
//            释放资源
            if(sqlSession!=null){
                sqlSession.close();
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
//        8.返回结果到控制层
        return page;
    }


    /*添加学生的方法*/
    @Override
    public void addStu(Student stu) {
        InputStream is=null;
        SqlSession sqlSession=null;


            try {
//        加载核心配置文件
                is= Resources.getResourceAsStream("MyBatisConfig.xml");
//            2.获取sqlsession对象工厂对象
                SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
//            3.通过sqlsession对象获取工厂对象
                sqlSession=build.openSession(true);
//            4.获取studentmapper接口的实现类对象
                StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//              5.调用实现类对象添加方法
                mapper.addStu(stu);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        /*修改学生的方法*/
    @Override
    public void updateStu(Student stu) {
        InputStream is =null;
        SqlSession sqlSession=null;
//        加载核心配置文件
//        获取sqlsession工厂对象用来获取sqlsession对象用来获取mapper映射对象用来调用其中的更新方法
        try {
             is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            mapper.updateStu(stu);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
//            关流
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sqlSession!=null){
                sqlSession.close();
            }
        }
    }

    /*删除学生的方法*/
    @Override
    public void deleteStu(String number) {
        InputStream is =null;
        SqlSession sqlSession=null;
//        加载核心配置文件
//        获取sqlsession工厂对象用来获取sqlsession对象用来获取mapper映射对象用来调用其中的更新方法
        try {
            is = Resources.getResourceAsStream("MyBatisConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
            sqlSession = build.openSession(true);
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            mapper.deleteStu(number);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
//            关流
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sqlSession!=null){
                sqlSession.close();
            }
        }
    }

}
